﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ASBCC.Database.Entity;
using ASBCC.Database.Service;

namespace ASBCC.Business
{
    public class ProcessBusiness
    {
        public ProcessBusiness()
        { }

        #region ProcessRegister(Flow_Process)
        /// <summary>
        /// 获得可使用的流程名称列表
        /// </summary>
        /// <returns></returns>
        public Flow_ProcessTableEntitySet GetAllEnabledProcessRegisterTable()
        {
            Flow_ProcessTableEntitySet es = Flow_ProcessManager.SelectAll();
            return es;
        }
        #endregion

        #region Flow_ProcessConfig
        /// <summary>
        /// 根据流程步骤ID获得流程配置实体
        /// </summary>
        /// <param name="configID"></param>
        /// <returns></returns>
        public Flow_ProcessConfigTableEntity GetProcessConfigEntityByKey(Guid configID)
        {
            return Flow_ProcessConfigManager.SelectByKeys(configID);
        }

        /// <summary>
        /// 获得全部流程配置
        /// </summary>
        /// <returns></returns>
        public Flow_ProcessConfigTableEntitySet GetAllProcessConfigTable()
        {
            Flow_ProcessConfigTableEntitySet es = Flow_ProcessConfigManager.SelectAll();
            return es;

        }

        /// <summary>
        /// 根据条件获得流程配置集
        /// </summary>
        /// <returns></returns>
        public Flow_ProcessConfigTableEntitySet GetProcessConfigTableByCondition(string condition)
        {
            Flow_ProcessConfigTableEntitySet es = Flow_ProcessConfigManager.SelectByCondition(condition);
            return es;

        }

        /// <summary>
        /// 通过configID删除流程配置步骤
        /// </summary>
        /// <param name="configID"></param>
        /// <returns></returns>
        public bool DeleteProcessConfigByID(string configID)
        {
            try
            {
                return Flow_ProcessConfigManager.Delete(new Guid(configID)) > 0;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 更新流程配置
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateProcessConfig(Flow_ProcessConfigTableEntity entity)
        {
            return Flow_ProcessConfigManager.Update(entity) > 0;
        }
        #endregion

        #region Union
        /// <summary>
        /// 更新流程配置
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="mailConfigListForInsert"></param>
        /// <param name="mailConfigListForUpdate"></param>
        /// <param name="mailConfigIDListForDelete"></param>
        /// <returns>是否更新成功</returns>
        public bool UpdateProcessConfigInfo(Flow_ProcessConfigTableEntity entity,
            List<Flow_ProcessMailConfigTableEntity> mailConfigListForInsert,
            List<Flow_ProcessMailConfigTableEntity> mailConfigListForUpdate,
            List<Guid> mailConfigIDListForDelete)
        {
            //Union with table [Flow_ProcessConfigTable] and table [Flow_ProcessMailConfig]
            bool isSuccess = true;

            //更新流程主配置
            isSuccess = UpdateProcessConfig(entity);

            //插入新流程邮件配置
            foreach (Flow_ProcessMailConfigTableEntity insertEntity in mailConfigListForInsert)
            {
                isSuccess = isSuccess && InsertFlowProcessMailConfig(insertEntity);
            }

            //更新流程邮件配置
            foreach (Flow_ProcessMailConfigTableEntity updateEntity in mailConfigListForUpdate)
            {
                isSuccess = isSuccess && UpdateFlowProcessMailConfig(updateEntity);
            }

            foreach (Guid id in mailConfigIDListForDelete)
            {
                isSuccess = isSuccess && DeleteFlowProcessMailConfig(id);
            }

            return isSuccess;
        }
        #endregion

        #region Flow_ProcessMailConfig
        /// <summary>
        /// 插入新流程邮件配置
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertFlowProcessMailConfig(Flow_ProcessMailConfigTableEntity entity)
        {
            return Flow_ProcessMailConfigManager.Insert(entity) > 0;
        }

        /// <summary>
        /// 更新新流程邮件配置
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateFlowProcessMailConfig(Flow_ProcessMailConfigTableEntity entity)
        {
            return Flow_ProcessMailConfigManager.Update(entity) > 0;
        }

        /// <summary>
        /// 删除新流程邮件配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteFlowProcessMailConfig(Guid id)
        {
            try
            {
                Flow_ProcessMailConfigManager.Delete(id);
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 根据条件获得邮件配置列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public Flow_ProcessMailConfigTableEntitySet GetMailConfigByCondition(string condition)
        {
            return Flow_ProcessMailConfigManager.SelectByCondition(condition);
        }

        /// <summary>
        /// 根据流程名称以及节点名称获得当前节点的邮件配置
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="activityName"></param>
        /// <returns></returns>
        public Flow_ProcessMailConfigTableEntitySet GetMailConfigByNodeInfo(string procName, string activityName)
        {
            return GetMailConfigByCondition(string.Format("ProcName='{0}' and ActivityName='{1}'", procName, activityName));
        }

        /// <summary>
        /// 根据流程名称以及节点名称获得当前节点的邮件配置(条件包括actionType)
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="activityName"></param>
        /// <param name="actionType"></param>
        /// <returns></returns>
        public Flow_ProcessMailConfigTableEntity GetMailConfigByNodeInfo(string procName, string activityName, string actionType)
        {
            Flow_ProcessMailConfigTableEntitySet es = GetMailConfigByCondition(string.Format("ProcName='{0}' and ActivityName='{1}' and ActionType='{2}'", procName, activityName, actionType));
            if (es.Count == 0)
                return null;
            return es[0];
        }
        #endregion

        #region Flow_Category
        /// <summary>
        /// 获得所有代理
        /// </summary>
        /// <returns></returns>
        public Flow_CategoryTableEntitySet GetAllDelegationCategoryTable()
        {
            Flow_CategoryTableEntitySet es = Flow_CategoryManager.SelectByCondition("DelegationRequired=1");
            return es;
        }
        #endregion

        #region Flow_ProcessLog
        /// <summary>
        /// 根据申请编号获得流程日志列表
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        public Flow_ProcessLogTableEntitySet GetProcessLogListBySn(string sn)
        {
            return Flow_ProcessLogManager.SelectByCondition(string.Format("ApplicationNo='{0}' and Disabled=0", sn));
        }

        /// <summary>
        /// 根据日志ID删除流程日志
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteProcLogByID(Guid id)
        {
            return Flow_ProcessLogManager.Delete(id) > 0;
        }

        /// <summary>
        /// 修改日志
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateProcLog(Flow_ProcessLogTableEntity entity)
        {
            return Flow_ProcessLogManager.Update(entity) > 0;
        }

        /// <summary>
        /// 新增日志
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertProcLog(Flow_ProcessLogTableEntity entity)
        {
            return Flow_ProcessLogManager.Insert(entity) > 0;
        }
        #endregion

        #region Flow_ProcessStamp
        /// <summary>
        /// 根据申请编号获得流程图章列表
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        public Flow_ProcessStampTableEntitySet GetProcessStampListBySn(string sn)
        {
            return Flow_ProcessStampManager.SelectByCondition(string.Format("ApplicationNo='{0}' and Disabled=0", sn));
        }

        /// <summary>
        /// 根据图章ID删除流程图章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteProcStampByID(Guid id)
        {
            return Flow_ProcessStampManager.Delete(id) > 0;
        }

        /// <summary>
        /// 修改图章
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateProcStamp(Flow_ProcessStampTableEntity entity)
        {
            return Flow_ProcessStampManager.Update(entity) > 0;
        }

        /// <summary>
        /// 新增图章
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertProcStamp(Flow_ProcessStampTableEntity entity)
        {
            return Flow_ProcessStampManager.Insert(entity) > 0;
        }
        #endregion

        #region Flow_ProcessLeadingTime
        /// <summary>
        /// 根据申请编号获得流程过程列表
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        public Flow_LeadingTimeTableEntitySet GetProcessLeadingTimeListBySn(string sn)
        {
            return Flow_LeadingTimeManager.SelectByCondition(string.Format("ApplicationNo='{0}' and Disabled=0", sn));
        }

        /// <summary>
        /// 根据过程ID删除过程
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteProcLeadingTimeByID(Guid id)
        {
            return Flow_LeadingTimeManager.Delete(id) > 0;
        }

        /// <summary>
        /// 修改过程
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateProcLeadingTime(Flow_LeadingTimeTableEntity entity)
        {
            return Flow_LeadingTimeManager.Update(entity) > 0;
        }

        /// <summary>
        /// 新增过程
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertProcLeadingTime(Flow_LeadingTimeTableEntity entity)
        {
            return Flow_LeadingTimeManager.Insert(entity) > 0;
        }
        #endregion

        #region MyActivity
        /// <summary>
        /// 根据条件获得代办事项列表
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="serialNumber"></param>
        /// <returns></returns>
        private MyActivityTableEntitySet GetMyActivityListByCondition(string condition)
        {
            return MyActivityManager.SelectByCondition(condition);
        }

        /// <summary>
        /// 根据条件获得代办事项列表
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="serialNumber"></param>
        /// <returns></returns>
        public MyActivityTableEntitySet GetMyActivityListByCondition(string procName, string applicationNo)
        {
            string condition = string.Format("ProcName='{0}'", procName);
            if (!string.IsNullOrEmpty(applicationNo))
            {
                condition = string.Format("{0} and ApplicationNo='{1}'", condition, applicationNo);
            }

            return GetMyActivityListByCondition(condition);
        }

        /// <summary>
        /// 根据OwnerName获得代办事项列表
        /// </summary>
        /// <param name="ownerName">ownerName,实际为Account</param>
        /// <returns></returns>
        public MyActivityTableEntitySet GetMyActivityListByOwnername(string ownerName)
        {
            string condition = string.Format("Owner='{0}'", ownerName);

            return GetMyActivityListByCondition(condition);
        }

        /// <summary>
        /// 根据OwnerName获得代办事项列表
        /// </summary>
        /// <param name="ownerName">ownerName,实际为Account</param>
        /// <returns></returns>
        public MyActivityTableEntitySet GetMyActivityList(string ownerEn, string procName, int formId, string applicationNo)
        {
            //string condition = "isdeleted=0";
            StringBuilder sb = new StringBuilder();
            sb.Append("1=1 ");
            if (!string.IsNullOrEmpty(ownerEn))
            {
                sb.AppendFormat("and OwnerEn like '%{0}%' ", ownerEn);
            }
            if (!string.IsNullOrEmpty(procName))
            {
                sb.AppendFormat("and ProcName='{0}' ", procName);
            }
            if (formId > 0)
            {
                sb.AppendFormat("and FormID={0} ", formId);
            }
            if (!string.IsNullOrEmpty(applicationNo))
            {
                sb.AppendFormat("and ApplicationNo like '%{0}%' ", applicationNo);
            }

            return GetMyActivityListByCondition(sb.ToString());
        }

        /// <summary>
        /// 根据ID获得代办事项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MyActivityTableEntity GetMyActivityInfoByID(Guid id)
        {
            return MyActivityManager.SelectByKeys(id);
        }

        /// <summary>
        /// 修改代办事项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool UpdateActivity(MyActivityTableEntity entity)
        {
            return MyActivityManager.Update(entity) > 0;
        }

        /// <summary>
        /// 根据ID删除代办事项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public void MoveMyActivity(List<Guid> myActivityIDList)
        {
            K2DataService.MoveMyActivityListToHistroy(myActivityIDList);
        }
        #endregion
    }
}
